<?php
class ModelConfigureOption extends Model {
    public function addOption($data) {
        $this->db->query("INSERT INTO `" . DB_PREFIX . "configure_option` SET type = '" . $this->db->escape($data['type']) . "', field_name = '" . $this->db->escape($data['field_name']) . "', sort_order = '" . (int)$data['sort_order'] . "'");

        $configureoption_id = $this->db->getLastId();

        foreach ($data['configureoption_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_description SET configureoption_id = '" . (int)$configureoption_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
        }

        if (isset($data['configureoption_value'])) {
            foreach ($data['configureoption_value'] as $configureoption_value) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_value SET configureoption_id = '" . (int)$configureoption_id . "', field_value = '" . $this->db->escape(html_entity_decode($configureoption_value['field_value'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$configureoption_value['sort_order'] . "'");

                $configureoption_value_id = $this->db->getLastId();

                foreach ($configureoption_value['configureoption_value_description'] as $language_id => $configureoption_value_description) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_value_description SET configureoption_value_id = '" . (int)$configureoption_value_id . "', language_id = '" . (int)$language_id . "', configureoption_id = '" . (int)$configureoption_id . "', name = '" . $this->db->escape($configureoption_value_description['name']) . "'");
                }
            }
        }
    }

    public function editOption($configureoption_id, $data) {
        $this->db->query("UPDATE `" . DB_PREFIX . "configure_option` SET field_name = '" . $this->db->escape($data['field_name']) . "',type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE configureoption_id = '" . (int)$configureoption_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "configure_option_description WHERE configureoption_id = '" . (int)$configureoption_id . "'");

        foreach ($data['configureoption_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_description SET configureoption_id = '" . (int)$configureoption_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "configure_option_value WHERE configureoption_id = '" . (int)$configureoption_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "configure_option_value_description WHERE configureoption_id = '" . (int)$configureoption_id . "'");
        if (isset($data['configureoption_value'])) {
            foreach ($data['configureoption_value'] as $configureoption_value) {
                if ($configureoption_value['configureoption_value_id']) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_value SET configureoption_value_id = '" . (int)$configureoption_value['configureoption_value_id'] . "', configureoption_id = '" . (int)$configureoption_id . "', field_value = '" . $this->db->escape(html_entity_decode($configureoption_value['field_value'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$configureoption_value['sort_order'] . "'");
                } else {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_value SET configureoption_id = '" . (int)$configureoption_id . "', field_value = '" . $this->db->escape(html_entity_decode($configureoption_value['field_value'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$configureoption_value['sort_order'] . "'");
                }

                $configureoption_value_id = $this->db->getLastId();

                foreach ($configureoption_value['configureoption_value_description'] as $language_id => $configureoption_value_description) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "configure_option_value_description SET configureoption_value_id = '" . (int)$configureoption_value_id . "', language_id = '" . (int)$language_id . "', configureoption_id = '" . (int)$configureoption_id . "', name = '" . $this->db->escape($configureoption_value_description['name']) . "'");
                }
            }
        }
    }

    public function deleteOption($configureoption_id) {
        $this->db->query("DELETE FROM `" . DB_PREFIX . "configure_option` WHERE configureoption_id = '" . (int)$configureoption_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "configure_option_description WHERE configureoption_id = '" . (int)$configureoption_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "configure_option_value WHERE configureoption_id = '" . (int)$configureoption_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "configure_option_value_description WHERE configureoption_id = '" . (int)$configureoption_id . "'");
    }

    public function getOption($configureoption_id) {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "configure_option` o LEFT JOIN " . DB_PREFIX . "configure_option_description od ON (o.configureoption_id = od.configureoption_id) WHERE o.configureoption_id = '" . (int)$configureoption_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        return $query->row;
    }

    public function getOptions($data = array()) {
        $sql = "SELECT * FROM `" . DB_PREFIX . "configure_option` o LEFT JOIN " . DB_PREFIX . "configure_option_description od ON (o.configureoption_id = od.configureoption_id) WHERE od.language_id = '" . (int)$this->config->get('config_language_id') . "'";

        if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
            $sql .= " AND od.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
        }

        $sort_data = array(
            'od.name',
            'o.type',
            'o.sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY od.name";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }


        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getOptionDescriptions($configureoption_id) {
        $option_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "configure_option_description WHERE configureoption_id = '" . (int)$configureoption_id . "'");

        foreach ($query->rows as $result) {
            $option_data[$result['language_id']] = array('name' => $result['name']);
        }

        return $option_data;
    }

    public function getOptionValue($configureoption_value_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "configure_option_value ov LEFT JOIN " . DB_PREFIX . "configure_option_value_description ovd ON (ov.configureoption_value_id = ovd.configureoption_value_id) WHERE ov.configureoption_value_id = '" . (int)$configureoption_value_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        return $query->row;
    }

    public function getOptionValues($configureoption_id) {
        $configureoption_value_data = array();

        $configureoption_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "configure_option_value ov LEFT JOIN " . DB_PREFIX . "configure_option_value_description ovd ON (ov.configureoption_value_id = ovd.configureoption_value_id) WHERE ov.configureoption_id = '" . (int)$configureoption_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order ASC");

        foreach ($configureoption_value_query->rows as $configureoption_value) {
            $configureoption_value_data[] = array(
                'configureoption_value_id' => $configureoption_value['configureoption_value_id'],
                'name'            => $configureoption_value['name'],
                'field_value'           => $configureoption_value['field_value'],
                'sort_order'      => $configureoption_value['sort_order']
            );
        }

        return $configureoption_value_data;
    }

    public function getOptionValueDescriptions($configureoption_id) {
        $configureoption_value_data = array();

        $configureoption_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "configure_option_value WHERE configureoption_id = '" . (int)$configureoption_id . "'");
        foreach ($configureoption_value_query->rows as $configureoption_value) {
            $configureoption_value_description_data = array();

            $configureoption_value_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "configure_option_value_description WHERE configureoption_value_id = '" . (int)$configureoption_value['configureoption_value_id'] . "'");
            foreach ($configureoption_value_description_query->rows as $configureoption_value_description) {
                $configureoption_value_description_data[$configureoption_value_description['language_id']] = array('name' => $configureoption_value_description['name']);
            }

            $configureoption_value_data[] = array(
                'configureoption_value_id'          => $configureoption_value['configureoption_value_id'],
                'configureoption_value_description' => $configureoption_value_description_data,
                'field_value'                    => $configureoption_value['field_value'],
                'sort_order'               => $configureoption_value['sort_order']
            );
        }

        return $configureoption_value_data;
    }

    public function getTotalOptions() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "configure_option`");

        return $query->row['total'];
    }
}
?>